package com.zjh.admin.sys.service.impl;

import com.zjh.admin.sys.mapper.ResourceMapper;
import com.zjh.admin.sys.model.ResourceEntity;
import com.zjh.admin.sys.service.ResourceService;
import com.zjh.core.base.baseservice.impl.AbstractService;
import com.zjh.core.exception.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @author ZWX
 * @Description: 角色service实现
 * @date 2018/11/12 17:44
 */
@Service("resourceService")
public class ResourceServiceImpl extends AbstractService<ResourceEntity , Long> implements ResourceService {

    @Autowired
    private ResourceMapper resourceMapper;

    @Autowired
    public void setBaseMapper() {
        super.setBaseMapper(resourceMapper);
    }

    /**
     * 获取用户ID对应的资源菜单信息
     *
     * @param userId
     * @return
     */
    @Override
    public List<ResourceEntity> findResourcesMenuByUserId(int userId) {
        return resourceMapper.findResourcesMenuByUserId(userId);
    }

    /**
     * 删除资源以及关联信息
     *
     * @param resourceIds
     * @return
     */
    @Override
    public boolean deleteRoleAndResource(List<Long> resourceIds) {
        try {
            resourceIds.forEach(resourceId -> {
                resourceMapper.deleteRolePerm(resourceId);
            });
            resourceMapper.deleteBatchById(resourceIds);
            //清空所有用户权限,重新加载权限
            return true;
        } catch (Exception e) {
            throw new ServiceException(e);
        }
    }

    /**
     * 分页查询 树形列表 资源信息
     *
     * @param parameter
     * @return
     */
    @Override
    public List<ResourceEntity> queryTreeGridListByPage(Map<String, Object> parameter) {
        return resourceMapper.queryTreeGridListByPage(parameter);
    }

    @Override
    public List<ResourceEntity> queryResourceList(Map<String, Object> parameter) {
        return resourceMapper.queryResourceList(parameter);
    }
}
